package com.youkeyi.ddy.cloud.basic.hr.employee.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.hr.employee.entity.TblEmployeeInterviewRecords;
import com.youkeyi.ddy.cloud.basic.hr.employee.service.TblEmployeeInterviewRecordsService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.HrEmployeeInterviewRecordsCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.dto.HrEmployeeInterviewRecordsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.dto.HrEmployeeInterviewRecordsListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.param.HrEmployeeInterviewRecordsCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.param.HrEmployeeInterviewRecordsDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.param.HrEmployeeInterviewRecordsQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.param.HrEmployeeInterviewRecordsUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 员工面试信息 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-06-13
 */
@Service
public class TblEmployeeInterviewRecordsController implements HrEmployeeInterviewRecordsCloudService {
    
    @Autowired
    private TblEmployeeInterviewRecordsService tblEmployeeInterviewRecordsService;

    @Override
    public HrEmployeeInterviewRecordsListCloudDto query(HrEmployeeInterviewRecordsQueryCloudParam param) {
        HrEmployeeInterviewRecordsListCloudDto dto = new HrEmployeeInterviewRecordsListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getEmployeeInterviewRecordsPrimaryKeys())) {
            param.getEmployeeInterviewRecordsPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getEmployeePrimaryKeys())) {
            param.getEmployeePrimaryKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblEmployeeInterviewRecords> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getEmployeeInterviewRecordsPrimaryKeys()), TblEmployeeInterviewRecords::getPrimaryKey, param.getEmployeeInterviewRecordsPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getEmployeePrimaryKeys()), TblEmployeeInterviewRecords::getEmployeeKey, param.getEmployeePrimaryKeys())
                .eq(StringUtils.isNotBlank(param.getInterviewDepartmentKey()), TblEmployeeInterviewRecords::getDepartmentKey, param.getInterviewDepartmentKey())
                .between(
                        null != param.getInterviewDateBegin() && null != param.getInterviewDateEnd(),
                        TblEmployeeInterviewRecords::getStartDate,
                        null != param.getInterviewDateBegin() ? new Date(param.getInterviewDateBegin()) : null,
                        null != param.getInterviewDateEnd() ? new Date(param.getInterviewDateEnd()) : null
                )
                .ne(TblEmployeeInterviewRecords::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblEmployeeInterviewRecords> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblEmployeeInterviewRecords> employeeList = tblEmployeeInterviewRecordsService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(employeeList.getRecords())) {
            dto.setTotal(employeeList.getTotal());

            List<HrEmployeeInterviewRecordsListItemCloudDto> items = new ArrayList<>();
            employeeList.getRecords().forEach(employee -> {
                HrEmployeeInterviewRecordsListItemCloudDto item = new HrEmployeeInterviewRecordsListItemCloudDto();
                item.setEmployeeInterviewRecordsPrimaryKey(employee.getPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setStartDate(employee.getStartDate() != null ? employee.getStartDate().getTime() : null);
                item.setInterviewTime(employee.getInterviewTime());
                item.setDepartmentKey(employee.getDepartmentKey());
                item.setDepartmentName(employee.getDepartmentName());
                item.setPositionKey(employee.getPositionKey());
                item.setPositionName(employee.getPositionName());
                item.setInterviewerKey(employee.getInterviewerKey());
                item.setInterviewerName(employee.getInterviewerName());
                item.setInterviewerStatus(employee.getInterviewerStatus());
                item.setInterviewComments(employee.getInterviewComments());
                item.setAnnexKey(employee.getAnnexKey());
                item.setRemark(employee.getRemark());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime() != null ? employee.getCreateTime().getTime() : null);
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime() != null ? employee.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(HrEmployeeInterviewRecordsCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getEmployeeKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblEmployeeInterviewRecords employee = new TblEmployeeInterviewRecords();
        employee.setPrimaryKey(SimpleUUidUtils.getUuid());
        employee.setEmployeeKey(param.getEmployeeKey());
        employee.setStartDate(param.getStartDate() != null ? new Date(param.getStartDate()) : null);
        employee.setInterviewTime(param.getInterviewTime());
        employee.setDepartmentKey(param.getDepartmentKey());
        employee.setDepartmentName(param.getDepartmentName());
        employee.setPositionKey(param.getPositionKey());
        employee.setPositionName(param.getPositionName());
        employee.setInterviewerKey(param.getInterviewerKey());
        employee.setInterviewerName(param.getInterviewerName());
        employee.setInterviewerStatus(param.getInterviewerStatus());
        employee.setInterviewComments(param.getInterviewComments());
        employee.setAnnexKey(param.getAnnexKey());
        employee.setRemark(param.getRemark());
        employee.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        employee.setCreateUserKey(param.getCreateUserKey());
        employee.setCreateUserName(param.getCreateUserName());
        employee.setCreateTime(new Date());
        boolean save = tblEmployeeInterviewRecordsService.save(employee);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(employee.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(HrEmployeeInterviewRecordsUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getEmployeeInterviewRecordsPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblEmployeeInterviewRecords> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblEmployeeInterviewRecords::getPrimaryKey, param.getEmployeeInterviewRecordsPrimaryKey());
        TblEmployeeInterviewRecords employee = new TblEmployeeInterviewRecords();
        employee.setPrimaryKey(param.getEmployeeInterviewRecordsPrimaryKey());
        employee.setStartDate(param.getStartDate() != null ? new Date(param.getStartDate()) : null);
        employee.setInterviewTime(param.getInterviewTime());
        employee.setDepartmentKey(param.getDepartmentKey());
        employee.setDepartmentName(param.getDepartmentName());
        employee.setPositionKey(param.getPositionKey());
        employee.setPositionName(param.getPositionName());
        employee.setInterviewerKey(param.getInterviewerKey());
        employee.setInterviewerName(param.getInterviewerName());
        employee.setInterviewerStatus(param.getInterviewerStatus());
        employee.setInterviewComments(param.getInterviewComments());
        employee.setAnnexKey(param.getAnnexKey());
        employee.setRemark(param.getRemark());
        employee.setUpdateUserKey(param.getUpdateUserKey());
        employee.setUpdateUserName(param.getUpdateUserName());
        employee.setUpdateTime(new Date());
        boolean save = tblEmployeeInterviewRecordsService.update(employee, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(HrEmployeeInterviewRecordsDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getEmployeeInterviewRecordsPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblEmployeeInterviewRecords> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblEmployeeInterviewRecords::getPrimaryKey, param.getEmployeeInterviewRecordsPrimaryKeys());

        TblEmployeeInterviewRecords employee = new TblEmployeeInterviewRecords();
        employee.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        employee.setUpdateUserKey(param.getUpdateUserKey());
        employee.setUpdateUserName(param.getUpdateUserName());
        employee.setUpdateTime(new Date());
        boolean save = tblEmployeeInterviewRecordsService.update(employee, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
